﻿using ICSharpCode.AvalonEdit.Rendering;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace editor_txt
{
    /// <summary>
    /// editor.xaml 的交互逻辑
    /// </summary>
    public partial class editor : UserControl
    {
        public editor()
        {
            InitializeComponent();
        }
        Document doc;
        bool initbk = false;
        public void Set(Document doc)
        {
            this.doc = doc;
            var extname = System.IO.Path.GetExtension(doc.filename);
            this.texteditor.SyntaxHighlighting = ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance.GetDefinitionByExtension(extname);
            this.texteditor.Text = System.IO.File.ReadAllText(doc.filename);

            if (initbk == false)
            {
                this.texteditor.TextArea.LeftMargins.Insert(0, new mymargin(this.texteditor));
                initbk = true;
                this.texteditor.TextArea.TextView.BackgroundRenderers.Add(new myback());
            }
        }
        class myback : IBackgroundRenderer
        {
            public KnownLayer Layer => KnownLayer.Background;

            public void Draw(TextView textView, DrawingContext drawingContext)
            {
                foreach (var line in textView.VisualLines)
                {
                    //if (textView.HighlightedLine == line.FirstDocumentLine.LineNumber)
                    if (line.FirstDocumentLine.LineNumber % 2 == 0)
                    {
                        var rect = BackgroundGeometryBuilder.GetRectsForSegment(textView, line.FirstDocumentLine);
                        foreach (var r in rect)
                        {
                            drawingContext.DrawRectangle(new SolidColorBrush(Color.FromArgb(0x05,0x3c, 0xaa, 0x3c)), null,
                                                         new Rect(r.Location, new Size(textView.ActualWidth, r.Height)));
                        }

                    }

                }
            }
        }

        class mymargin : ICSharpCode.AvalonEdit.Editing.LineNumberMargin
        {
            ICSharpCode.AvalonEdit.TextEditor editor;
            public mymargin(ICSharpCode.AvalonEdit.TextEditor editor)
            {
                this.editor = editor;

            }
            protected override Size MeasureOverride(Size availableSize)
            {
                return new Size(14, availableSize.Height);
            }
            protected override void OnRender(DrawingContext drawingContext)
            {
                //base.OnRender(drawingContext);
                Size renderSize = this.RenderSize;
                var lines = this.editor.TextArea.TextView.VisualLines;
                var y = this.editor.TextArea.TextView.ScrollOffset.Y;
                double xcenter = (14 - 10) / 2;
                foreach (ICSharpCode.AvalonEdit.Rendering.VisualLine l in lines)
                {
                    var ycenter = (l.Height - 10) / 2;
                    var outr = new Rect(xcenter, l.VisualTop - y + ycenter, 10, 10);
                    drawingContext.DrawRoundedRectangle(SystemColors.HighlightBrush, null,
                            outr, 5, 5);

                    //var linenum = l.FirstDocumentLine.LineNumber;
                    //if (linenum < 100)
                    //{
                    //    var formattedText = new FormattedText(linenum.ToString(), System.Globalization.CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface("Verdana"), 8, Brushes.Black);
                    //    drawingContext.DrawText(formattedText, new Point(0, l.VisualTop - y));
                    //}

                }


            }
        }
        public void Save()
        {
            var txt = this.texteditor.Text;
            System.IO.File.Delete(doc.filename);
            System.IO.File.WriteAllText(doc.filename, txt);
        }
        public void Load()
        {
            this.texteditor.Text = System.IO.File.ReadAllText(doc.filename);

        }

        private void texteditor_TextChanged(object sender, EventArgs e)
        {
            doc.Edit();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}
